package com.tmq.diancan.controller;

import com.tmq.diancan.entity.UserInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.DigestUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.rmi.runtime.Log;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @Author tangmqb DCITS
 * @DATE 2021/3/17 17:25
 * @Version 1.0.0
 */
@RestController
@AllArgsConstructor
@Slf4j
@RequestMapping("/wx/message")
public class WxMessageController {



    @GetMapping("/listen")
    public R<Boolean> userInfo(String signature, String timestamp, String nonce,String echostr) {
        log.info("get message:\nsignature:{}\ntimestamp:{}\nnonce:{}\nechostr:{}",signature,timestamp,nonce,echostr);
//       获取
        String[] arr = {"tmq",timestamp,nonce};
        List<String> list = Arrays.asList(arr);
        Collections.sort(list);
        log.info("字典序排序后:{}",list);
        StringBuffer str =new StringBuffer();
        for (int i = 0; i < list.size(); i++) {
            str.append(list.get(i));
        }
        //sha1加密
        String shaStr = DigestUtil.sha1(str.toString());
        log.info("sha1加密前:{}，sha1加密后:{}",str,shaStr);
        if(Func.equals(signature,shaStr)){
            log.info("解析成功");
        }else {
            log.info("解析失败");
        }
        return R.data(true);
    }
}
